//  引入全局变量
// const app = getApp();
// //  引入网络请求工具类
// import request from '../../utils/request.js'
// //  引入接口路径
// import {
//   queryProductPageApi,
//   queryProductInfoApi,
//   queryTypeLevel1Api,
//   queryTypeLevel2Api,
//   queryLabelTypeApi
// } from '../../api/index/product.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: [{
      id: 1,
      name: '分类1'
    }, {
      id: 1,
      name: '分类2'
    }],
    page: 1,
    list: [],
    productList: [{
      productName: '阿三大规划感觉好贵激活工具关环境苏打',
      productScore: '130'
    }, {
      productName: '阿三大规划感觉好贵激活工具关环境苏打',
      productScore: '130'
    }, {
      productName: '阿三大规划感觉好贵激活工具关环境苏打',
      productScore: '130'
    }],
    name: '',
    total: 1,
    limit: 10,
    length: '',
    typeId: '',
    tabIndex: 0,
    navStyle: '',
    headStyle: '',
    logo: '',
    searchStatus: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.queryTypeList()
  },
  queryTypeList() {
    wx.showLoading({
      title: '加载中...'
    });
    let param = {
      label: 3
    }
    request.post(queryLabelTypeApi, param).then((res) => {
      let typeArr = [{
        typeId: '',
        name: '全部'
      }]
      this.setData({
        type: [...typeArr, ...res.data.data],
        typeId: ""
      })
      // this.queryProductPage();
    })
  },
  /**
   * 查询产品列表
   */
  queryProductPage() {
    let param = {
      label: 3,
      name: this.data.name,
      page: this.data.page,
      limit: this.data.limit,
      smallTypeId: this.data.typeId,
    }
    request.post(queryProductPageApi, param).then((res) => {
      if (res.data.data.records.length == 0) {
        this.setData({
          searchStatus: false
        })
        return false,
          wx.hideLoading();
      }
      for (let key in res.data.data.records) {
        res.data.data.records[key].imgUrl = app.globalData.baseUrl + res.data.data.records[key].productCoverPath;
      }
      this.setData({
        length: res.data.data.records.length,
        productList: [...this.data.productList, ...res.data.data.records],
        total: res.data.data.total,
        searchStatus: true
      })
      wx.hideLoading();
    })
  },
  // 输入框改变事件
  bindTitle(e) {
    this.setData({
      name: e.detail.value
    })
    this.search()
  },
  // 防抖函数
  debounce: function (fn, delay) {
    var timer = null
    return function () {
      clearTimeout(timer)
      timer = setTimeout(function () {
        fn.apply(this, arguments)
      }, delay)
    }
  },
  /**
   * 搜索确认
   * @param {*} e 
   */
  search(e) {
    this.setData({
      productList: [],
      page: 1
    })
    this.queryProductPage()
  },
  /**
   * 切换tab页
   * @param {q} e 
   */
  onTabClick(e) {
    let id = e.currentTarget.dataset.id;
    this.data.type.forEach((item, index) => {
      if (item.id == id) {
        this.setData({
          tabIndex: index
        })
      }
    });
    if (id == undefined) {
      id = ''
    }
    this.setData({
      typeId: id,
      productList: [],
      page: 1,
    })
    this.queryProductPage();
  },
  /**
   * 跳转详情页
   */
  onProductInfo(e) {
    let id = e.currentTarget.dataset.id;
    let title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/pages/productInfoTraining/productInfo?id=' + id + '&title=' + title, // 指定要跳转到详情页
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh() {
  //   const length = this.data.res.data.data.records.length
  //   const total = this.data.res.data.data.total
  //   if (length < total) {
  //     this.data.page++
  //     this.queryProductList()
  //   }
  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const length = this.data.length
    const total = this.data.total
    if (length < total) {
      this.data.page++
      this.queryProductPage()
    }
  },
})